package com.lagou.task02;

public class SameSubString {

    public String getLongestSubString(String str1, String str2){
        if (str1.length() < str2.length()){
            String s = str1;
            str1 = str2;
            str2 = s;
        }

        if (str1.contains(str2)){
            return str2;
        }

        String targetStr = null;
        for (int i = str2.length()-1; i > 0; i--) {
            int left = 0;
            int right = i;
            while (right <= str2.length()){
                String s = str2.substring(left,right);
                System.out.println("子串起始下标：[" + left + "," + right + ")" + " = " + s);
                if (str1.contains(s)){
                    targetStr = s;
                    break;
                }
                left++;
                right++;
            }
            if (targetStr != null){
                break;
            }
        }

        return targetStr;
    }
}
